package com.hmall.search.mq;

import com.hmall.search.service.ISearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

import static com.hmall.common.constants.MqConstants.*;

@Component
public class ItemListener {

    @Autowired
    private ISearchService searchService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = HMALL_ITEM_UP_QUEUE),
            exchange = @Exchange(name = HMALL_ITEM_TOPIC, type = ExchangeTypes.TOPIC),
            key = HMALL_ITEM_UP_KEY
    ))
    public void listenItemUp(Map<String, Object> message){
        searchService.saveById(message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = HMALL_ITEM_DOWN_QUEUE),
            exchange = @Exchange(name = HMALL_ITEM_TOPIC, type = ExchangeTypes.TOPIC),
            key = HMALL_ITEM_DOWN_KEY
    ))
    public void listenItemDown(Long id){
        searchService.deleteById(id);
    }
}
